package com.android.gufengmall.video;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CarouselAdapter;
import com.android.gufengmall.adapter.CarouselTitleAdapter;
import com.android.gufengmall.adapter.VideoAdapter;
import com.android.gufengmall.bean.CarouselBean;
import com.android.gufengmall.bean.VideoBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.VideoBeanDao;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.BannerUtils;

import java.util.ArrayList;
import java.util.List;

public class VideoFragment extends Fragment implements VideoAdapter.VideoListener {

    private static final String TAG = "VideoFragment";

    private CarouselAdapter mCarouselAdapter;
    private Banner mBanner;
    private VideoAdapter mVideoAdapter;
    private List<VideoBean> mVideoBeanList;

    private App mApp;
    private DaoSession mDaoSession;
    private VideoBeanDao mVideoBeanDao;

    private EditText mEdtSearch;
    private TabLayout mTabLayout;
    private TabItem mTabItemWx;
    private TabItem mTabItemFs;
    private TabItem mTabItemGy;
    private TabItem mTabItemQq;
    private RecyclerView mRvVideo;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_video, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    private void initData() {
        mVideoBeanList = new ArrayList<>();

        mApp = (App) getActivity().getApplication();
        mDaoSession = mApp.getDaoSession();
        mVideoBeanDao = mDaoSession.getVideoBeanDao();

        List<VideoBean> videoBeans = mVideoBeanDao.loadAll();
        mVideoBeanList.addAll(videoBeans);

        //自定义的图片适配器，也可以使用默认的BannerImageAdapter
        mCarouselAdapter = new CarouselAdapter(CarouselBean.getCarouselData());
        mBanner.setAdapter(mCarouselAdapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(requireContext()))//设置指示器
                .setOnBannerListener((data, position) -> {
                    Snackbar.make(mBanner, ((CarouselBean) data).title, Snackbar.LENGTH_SHORT).show();
                });

        mBanner.setAdapter(new CarouselTitleAdapter(CarouselBean.getCarouselData()));
        mBanner.setIndicator(new CircleIndicator(requireContext()));
        mBanner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
        mBanner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
                BannerConfig.INDICATOR_MARGIN, BannerUtils.dp2px(12)));

        mRvVideo.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        mVideoAdapter = new VideoAdapter(requireContext(), mVideoBeanList);
        mRvVideo.setAdapter(mVideoAdapter);
    }

    private void initView(View root) {
        mEdtSearch = root.findViewById(R.id.edt_search);
        mTabLayout = root.findViewById(R.id.tab_layout);
        mTabItemWx = root.findViewById(R.id.tab_item_wx);
        mTabItemFs = root.findViewById(R.id.tab_item_fs);
        mTabItemGy = root.findViewById(R.id.tab_item_gy);
        mTabItemQq = root.findViewById(R.id.tab_item_qq);
        mBanner = root.findViewById(R.id.banner);
        mRvVideo = root.findViewById(R.id.rv_video);
    }

    private void initListener() {
        mVideoAdapter.setOnVideoListener(this);
        mEdtSearch.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            intent.putExtra("isVideo", true);
            startActivity(intent);
        });
    }

    @Override
    public void onVideoClick(String position) {
        Intent intent = new Intent(getActivity(), VideoDetailsActivity.class);
        intent.putExtra("position", position);
        startActivity(intent);
    }

    @Override
    public void onLikeListener(String position) {
        List<VideoBean> videoBeanList = mVideoBeanDao.queryBuilder().where(VideoBeanDao.Properties.VideoTitle.eq(position)).list();
        for (int i = 0; i < videoBeanList.size(); i++) {
            VideoBean videoBean = videoBeanList.get(i);
            if (videoBean.getVideoTitle().equals(position)) {
                boolean isLike = videoBean.getIsLike();
                videoBean.setIsLike(!isLike);
                mVideoBeanDao.update(videoBean);
                if (isLike) {
                    Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
                }
                mVideoAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onCollectListener(String position) {
        List<VideoBean> videoBeanList = mVideoBeanDao.queryBuilder().where(VideoBeanDao.Properties.VideoTitle.eq(position)).list();
        for (int i = 0; i < videoBeanList.size(); i++) {
            VideoBean videoBean = videoBeanList.get(i);
            if (videoBean.getVideoTitle().equals(position)) {
                boolean isCollect = videoBean.getIsCollect();
                videoBean.setIsCollect(!isCollect);
                mVideoBeanDao.update(videoBean);
                if (isCollect) {
                    Toast.makeText(requireContext(), "取消收藏", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(requireContext(), "收藏成功", Toast.LENGTH_SHORT).show();
                }
                mVideoAdapter.notifyDataSetChanged();
            }
        }
    }
}